package com.jnu.myfirstapplication;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;


public class MainActivity extends AppCompatActivity{
    private ArrayList<String> mainStringSet;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        RecyclerView recyclerViewMain=findViewById(R.id.viewpager2_main);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
//        recyclerViewMain.setLayoutManager(linearLayoutManager);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);



        mainStringSet=new ArrayList<>();
//        for(int i=0;i<3;i++){
//            mainStringSet.add("item"+i);
//        }

        MainRecycleViewAdapter mainRecycleViewAdapter=new MainRecycleViewAdapter(mainStringSet);
//        recyclerViewMain.setAdapter(mainRecycleViewAdapter);
    }


    public class MainRecycleViewAdapter extends RecyclerView.Adapter<MainRecycleViewAdapter.ViewHolder> {

        private ArrayList<String> localDataSet;

        /**
         * Provide a reference to the type of views that you are using
         * (custom ViewHolder).
         */
        public class ViewHolder extends RecyclerView.ViewHolder {
            private final TextView textView;
            private final ImageView imageViewImage;

            public ViewHolder(View view) {
                super(view);
                // Define click listener for the ViewHolder's View

                imageViewImage = view.findViewById(R.id.image_view_book_cover);
                textView = (TextView) view.findViewById(R.id.text_view_book_title);
            }

            public TextView getTextView() {
                return textView;
            }

            public ImageView getImageViewImage() {
                return imageViewImage;
            }
        }

        /**
         * Initialize the dataset of the Adapter.
         *
         * @param dataSet String[] containing the data to populate views to be used
         * by RecyclerView.
         */
        public MainRecycleViewAdapter(ArrayList<String> dataSet) {
            localDataSet = dataSet;
        }

        // Create new views (invoked by the layout manager)
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            // Create a new view, which defines the UI of the list item
            View view = LayoutInflater.from(viewGroup.getContext())
                    .inflate(R.layout.image_main, viewGroup, false);

            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ViewHolder viewHolder, final int position) {
            viewHolder.getTextView().setText(localDataSet.get(position));
            if(position==1){
                viewHolder.getImageViewImage().setImageResource(R.drawable.book_1);
            }else if(position==2){
                viewHolder.getImageViewImage().setImageResource(R.drawable.book_2);
            }else{
                viewHolder.getImageViewImage().setImageResource(R.drawable.book_no_name);
            }
        }

        // Replace the contents of a view (invoked by the layout manager)


        // Return the size of your dataset (invoked by the layout manager)
        @Override
        public int getItemCount() {
            return localDataSet.size();
        }
    }


//    //通过添加监听属性
//    public void doClick(View view){
//
//        TextView textView1 = findViewById(R.id.textView);
//        TextView textView2 = findViewById(R.id.textView2);
//        textView1.setText(textView2.getText());
//        textView2.setText(textView1.getText());
//    }
//
//    //通过内部接口进行监听
//    @Override
//    public void onClick(View view) {
//
//        TextView textView1 = findViewById(R.id.textView);
//        TextView textView2 = findViewById(R.id.textView2);
//        textView1.setText(textView2.getText());
//        textView2.setText("101112");
//    }
//    //新建外部类进行监听
//    class MyListener extends AppCompatActivity implements View.OnClickListener {
//        @Override
//        public void onClick(View v) {
//            TextView textView1 = findViewById(R.id.textView);
//            TextView textView2 = findViewById(R.id.textView2);
//            CharSequence x=textView1.getText();
//            textView1.setText(textView2.getText());
//            textView2.setText(x);
//        }
//    }
}

